package com.example.myapplication;

import android.graphics.Color;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

public class Activity_UC3 extends AppCompatActivity implements View.OnClickListener{

    protected Timer timer;
    protected int BeginIndex=0;

    protected int[] Collect_Color;
    protected ImageView[] Collect_ImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__uc3);

        Collect_Color=new int[]{
                Color.rgb(160,160,160),
                Color.rgb(120,120,120),
                Color.rgb(80,80,80),
                Color.rgb(40,40,40),
                Color.rgb(0,0,0),
        };
        Collect_ImageView=new ImageView[]{
                (ImageView)findViewById(R.id.IV_UC3_0),
                (ImageView)findViewById(R.id.IV_UC3_1),
                (ImageView)findViewById(R.id.IV_UC3_2),
                (ImageView)findViewById(R.id.IV_UC3_3),
                (ImageView)findViewById(R.id.IV_UC3_4)
        };

        for(int i=0;i<5;i++){
            Collect_ImageView[i].setBackgroundColor(Collect_Color[i]);
        }

        final Handler handler=new Handler(){
            @Override
            public void handleMessage(Message msg){
                if(msg.what==0x1){
                    for(int i=0;i<5;i++){
                        Collect_ImageView[(i+BeginIndex)%5].setBackgroundColor(Collect_Color[i]);
                    }
                    BeginIndex=(BeginIndex+1)%5;
                }
                super.handleMessage(msg);
            }
        };
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Message msg=new Message();
                msg.what=0x1;
                handler.sendMessage(msg);
            }
        },0,1000);
    }

    public void onClick(View v){
        Intent intent=new Intent();
        switch (v.getId()){
            case R.id.btn_ReturnMain:
                intent.setClass(this,MainActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        timer.cancel();
        timer=null;
    }
}
